<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.text &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-text">
            
  <h1>Source code for django.utils.text</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">unicodedata</span>
<span class="kn">from</span> <span class="nn">gzip</span> <span class="kn">import</span> <span class="n">GzipFile</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango19Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="kn">import</span> <span class="n">allow_lazy</span><span class="p">,</span> <span class="n">SimpleLazyObject</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves</span> <span class="kn">import</span> <span class="n">html_entities</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span><span class="p">,</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">pgettext</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="kn">import</span> <span class="n">mark_safe</span>

<span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
    <span class="c"># Import force_unicode even though this module doesn&#39;t use it, because some</span>
    <span class="c"># people rely on it being here.</span>
    <span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_unicode</span>  <span class="c"># NOQA</span>


<span class="c"># Capitalizes the first letter of a string.</span>
<span class="n">capfirst</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="ow">and</span> <span class="n">force_text</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">+</span> <span class="n">force_text</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">capfirst</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">capfirst</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>

<span class="c"># Set up regular expressions</span>
<span class="n">re_words</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;&lt;.*?&gt;|((?:\w[-\w]*|&amp;.*?;)+)&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">U</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">re_chars</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;&lt;.*?&gt;|(.)&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">U</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">re_tag</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;&lt;(/)?([^ ]+?)(?:(\s*/)| .*?)?&gt;&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">re_newlines</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;\r\n|\r&#39;</span><span class="p">)</span>  <span class="c"># Used in normalize_newlines</span>
<span class="n">re_camel_case</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(((?&lt;=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">width</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A word-wrap function that preserves existing line breaks and most spaces in</span>
<span class="sd">    the text. Expects that existing line breaks are posix newlines.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_generator</span><span class="p">():</span>
        <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">))</span>
        <span class="n">word</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
        <span class="k">yield</span> <span class="n">word</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="o">-</span> <span class="n">word</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
            <span class="k">if</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="ow">in</span> <span class="n">word</span><span class="p">:</span>
                <span class="n">lines</span> <span class="o">=</span> <span class="n">word</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lines</span> <span class="o">=</span> <span class="p">(</span><span class="n">word</span><span class="p">,)</span>
            <span class="n">pos</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">pos</span> <span class="o">&gt;</span> <span class="n">width</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
                <span class="n">pos</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">yield</span> <span class="s">&#39; &#39;</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="n">pos</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">yield</span> <span class="n">word</span>
    <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_generator</span><span class="p">())</span>
<span class="n">wrap</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">wrap</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Truncator</span><span class="p">(</span><span class="n">SimpleLazyObject</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An object used to truncate text, either by characters or words.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Truncator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">add_truncation_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">truncate</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">truncate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">truncate</span> <span class="o">=</span> <span class="n">pgettext</span><span class="p">(</span>
                <span class="s">&#39;String to return when truncating text&#39;</span><span class="p">,</span>
                <span class="s">&#39;</span><span class="si">%(truncated_text)s</span><span class="s">...&#39;</span><span class="p">)</span>
        <span class="n">truncate</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">truncate</span><span class="p">)</span>
        <span class="k">if</span> <span class="s">&#39;</span><span class="si">%(truncated_text)s</span><span class="s">&#39;</span> <span class="ow">in</span> <span class="n">truncate</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">truncate</span> <span class="o">%</span> <span class="p">{</span><span class="s">&#39;truncated_text&#39;</span><span class="p">:</span> <span class="n">text</span><span class="p">}</span>
        <span class="c"># The truncation text didn&#39;t contain the %(truncated_text)s string</span>
        <span class="c"># replacement argument so just append it to the text.</span>
        <span class="k">if</span> <span class="n">text</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">truncate</span><span class="p">):</span>
            <span class="c"># But don&#39;t append the truncation text if the current text already</span>
            <span class="c"># ends in this.</span>
            <span class="k">return</span> <span class="n">text</span>
        <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">truncate</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">chars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">truncate</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">html</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the text truncated to be no longer than the specified number</span>
<span class="sd">        of characters.</span>

<span class="sd">        Takes an optional argument of what should be used to notify that the</span>
<span class="sd">        string has been truncated, defaulting to a translatable string of an</span>
<span class="sd">        ellipsis (...).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">unicodedata</span><span class="o">.</span><span class="n">normalize</span><span class="p">(</span><span class="s">&#39;NFC&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">)</span>

        <span class="c"># Calculate the length to truncate to (max length - end_text length)</span>
        <span class="n">truncate_len</span> <span class="o">=</span> <span class="n">length</span>
        <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_truncation_text</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">truncate</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">unicodedata</span><span class="o">.</span><span class="n">combining</span><span class="p">(</span><span class="n">char</span><span class="p">):</span>
                <span class="n">truncate_len</span> <span class="o">-=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="n">truncate_len</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">break</span>
        <span class="k">if</span> <span class="n">html</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_truncate_html</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">truncate_len</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_text_chars</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">truncate_len</span><span class="p">)</span>
    <span class="n">chars</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">chars</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_text_chars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">truncate_len</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Truncates a string after a certain number of chars.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s_len</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">end_index</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">char</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">unicodedata</span><span class="o">.</span><span class="n">combining</span><span class="p">(</span><span class="n">char</span><span class="p">):</span>
                <span class="c"># Don&#39;t consider combining characters</span>
                <span class="c"># as adding to the string length</span>
                <span class="k">continue</span>
            <span class="n">s_len</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="n">end_index</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">s_len</span> <span class="o">&gt;</span> <span class="n">truncate_len</span><span class="p">:</span>
                <span class="n">end_index</span> <span class="o">=</span> <span class="n">i</span>
            <span class="k">if</span> <span class="n">s_len</span> <span class="o">&gt;</span> <span class="n">length</span><span class="p">:</span>
                <span class="c"># Return the truncated string</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_truncation_text</span><span class="p">(</span><span class="n">text</span><span class="p">[:</span><span class="n">end_index</span> <span class="ow">or</span> <span class="mi">0</span><span class="p">],</span>
                                                <span class="n">truncate</span><span class="p">)</span>

        <span class="c"># Return the original string since no truncation was necessary</span>
        <span class="k">return</span> <span class="n">text</span>

    <span class="k">def</span> <span class="nf">words</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">truncate</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">html</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Truncates a string after a certain number of words. Takes an optional</span>
<span class="sd">        argument of what should be used to notify that the string has been</span>
<span class="sd">        truncated, defaulting to ellipsis (...).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">html</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_truncate_html</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_text_words</span><span class="p">(</span><span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_text_words</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Truncates a string after a certain number of words.</span>

<span class="sd">        Newlines in the string will be stripped.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">words</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wrapped</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">words</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">length</span><span class="p">:</span>
            <span class="n">words</span> <span class="o">=</span> <span class="n">words</span><span class="p">[:</span><span class="n">length</span><span class="p">]</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_truncation_text</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">),</span> <span class="n">truncate</span><span class="p">)</span>
        <span class="k">return</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_truncate_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">truncate</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">truncate_len</span><span class="p">,</span> <span class="n">words</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Truncates HTML to a certain number of chars (not counting tags and</span>
<span class="sd">        comments), or, if words is True, then to a certain number of words.</span>
<span class="sd">        Closes opened tags if they were correctly closed in the given HTML.</span>

<span class="sd">        Newlines in the HTML are preserved.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">words</span> <span class="ow">and</span> <span class="n">length</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&#39;&#39;</span>

        <span class="n">html4_singlets</span> <span class="o">=</span> <span class="p">(</span>
            <span class="s">&#39;br&#39;</span><span class="p">,</span> <span class="s">&#39;col&#39;</span><span class="p">,</span> <span class="s">&#39;link&#39;</span><span class="p">,</span> <span class="s">&#39;base&#39;</span><span class="p">,</span> <span class="s">&#39;img&#39;</span><span class="p">,</span>
            <span class="s">&#39;param&#39;</span><span class="p">,</span> <span class="s">&#39;area&#39;</span><span class="p">,</span> <span class="s">&#39;hr&#39;</span><span class="p">,</span> <span class="s">&#39;input&#39;</span>
        <span class="p">)</span>

        <span class="c"># Count non-HTML chars/words and keep note of open tags</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">end_text_pos</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">current_len</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">open_tags</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">regex</span> <span class="o">=</span> <span class="n">re_words</span> <span class="k">if</span> <span class="n">words</span> <span class="k">else</span> <span class="n">re_chars</span>

        <span class="k">while</span> <span class="n">current_len</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">:</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">m</span><span class="p">:</span>
                <span class="c"># Checked through whole string</span>
                <span class="k">break</span>
            <span class="n">pos</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">end</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
                <span class="c"># It&#39;s an actual non-HTML word or char</span>
                <span class="n">current_len</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="n">current_len</span> <span class="o">==</span> <span class="n">truncate_len</span><span class="p">:</span>
                    <span class="n">end_text_pos</span> <span class="o">=</span> <span class="n">pos</span>
                <span class="k">continue</span>
            <span class="c"># Check for tag</span>
            <span class="n">tag</span> <span class="o">=</span> <span class="n">re_tag</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">tag</span> <span class="ow">or</span> <span class="n">current_len</span> <span class="o">&gt;=</span> <span class="n">truncate_len</span><span class="p">:</span>
                <span class="c"># Don&#39;t worry about non tags or tags after our truncate point</span>
                <span class="k">continue</span>
            <span class="n">closing_tag</span><span class="p">,</span> <span class="n">tagname</span><span class="p">,</span> <span class="n">self_closing</span> <span class="o">=</span> <span class="n">tag</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span>
            <span class="c"># Element names are always case-insensitive</span>
            <span class="n">tagname</span> <span class="o">=</span> <span class="n">tagname</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">self_closing</span> <span class="ow">or</span> <span class="n">tagname</span> <span class="ow">in</span> <span class="n">html4_singlets</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">elif</span> <span class="n">closing_tag</span><span class="p">:</span>
                <span class="c"># Check for match in open tags list</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">i</span> <span class="o">=</span> <span class="n">open_tags</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">tagname</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c"># SGML: An end tag closes, back to the matching start tag,</span>
                    <span class="c"># all unclosed intervening start tags with omitted end tags</span>
                    <span class="n">open_tags</span> <span class="o">=</span> <span class="n">open_tags</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># Add it to the start of the open tags list</span>
                <span class="n">open_tags</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">tagname</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">current_len</span> <span class="o">&lt;=</span> <span class="n">length</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">text</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">text</span><span class="p">[:</span><span class="n">end_text_pos</span><span class="p">]</span>
        <span class="n">truncate_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">add_truncation_text</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">truncate</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">truncate_text</span><span class="p">:</span>
            <span class="n">out</span> <span class="o">+=</span> <span class="n">truncate_text</span>
        <span class="c"># Close any tags still open</span>
        <span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">open_tags</span><span class="p">:</span>
            <span class="n">out</span> <span class="o">+=</span> <span class="s">&#39;&lt;/</span><span class="si">%s</span><span class="s">&gt;&#39;</span> <span class="o">%</span> <span class="n">tag</span>
        <span class="c"># Return string</span>
        <span class="k">return</span> <span class="n">out</span>


<span class="k">def</span> <span class="nf">get_valid_filename</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the given string converted to a string that can be used for a clean</span>
<span class="sd">    filename. Specifically, leading and trailing spaces are removed; other</span>
<span class="sd">    spaces are converted to underscores; and anything that is not a unicode</span>
<span class="sd">    alphanumeric, dash, underscore, or dot, is removed.</span>
<span class="sd">    &gt;&gt;&gt; get_valid_filename(&quot;john&#39;s portrait in 2004.jpg&quot;)</span>
<span class="sd">    &#39;johns_portrait_in_2004.jpg&#39;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span> <span class="s">&#39;_&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;(?u)[^-\w.]&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="n">get_valid_filename</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">get_valid_filename</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_text_list</span><span class="p">(</span><span class="n">list_</span><span class="p">,</span> <span class="n">last_word</span><span class="o">=</span><span class="n">ugettext_lazy</span><span class="p">(</span><span class="s">&#39;or&#39;</span><span class="p">)):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    &gt;&gt;&gt; get_text_list([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;])</span>
<span class="sd">    &#39;a, b, c or d&#39;</span>
<span class="sd">    &gt;&gt;&gt; get_text_list([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], &#39;and&#39;)</span>
<span class="sd">    &#39;a, b and c&#39;</span>
<span class="sd">    &gt;&gt;&gt; get_text_list([&#39;a&#39;, &#39;b&#39;], &#39;and&#39;)</span>
<span class="sd">    &#39;a and b&#39;</span>
<span class="sd">    &gt;&gt;&gt; get_text_list([&#39;a&#39;])</span>
<span class="sd">    &#39;a&#39;</span>
<span class="sd">    &gt;&gt;&gt; get_text_list([])</span>
<span class="sd">    &#39;&#39;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">list_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="s">&#39;&#39;</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">list_</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">list_</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="c"># Translators: This string is used as a separator between list elements</span>
        <span class="n">_</span><span class="p">(</span><span class="s">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">list_</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span>
        <span class="n">force_text</span><span class="p">(</span><span class="n">last_word</span><span class="p">),</span> <span class="n">force_text</span><span class="p">(</span><span class="n">list_</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">get_text_list</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">get_text_list</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">normalize_newlines</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Normalizes CRLF and CR newlines to just LF.&quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">re_newlines</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="n">normalize_newlines</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">normalize_newlines</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">phone2numeric</span><span class="p">(</span><span class="n">phone</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts a phone number with letters into its numeric equivalent.&quot;&quot;&quot;</span>
    <span class="n">char2number</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="s">&#39;2&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">:</span> <span class="s">&#39;2&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">:</span> <span class="s">&#39;2&#39;</span><span class="p">,</span> <span class="s">&#39;d&#39;</span><span class="p">:</span> <span class="s">&#39;3&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">:</span> <span class="s">&#39;3&#39;</span><span class="p">,</span> <span class="s">&#39;f&#39;</span><span class="p">:</span> <span class="s">&#39;3&#39;</span><span class="p">,</span>
         <span class="s">&#39;g&#39;</span><span class="p">:</span> <span class="s">&#39;4&#39;</span><span class="p">,</span> <span class="s">&#39;h&#39;</span><span class="p">:</span> <span class="s">&#39;4&#39;</span><span class="p">,</span> <span class="s">&#39;i&#39;</span><span class="p">:</span> <span class="s">&#39;4&#39;</span><span class="p">,</span> <span class="s">&#39;j&#39;</span><span class="p">:</span> <span class="s">&#39;5&#39;</span><span class="p">,</span> <span class="s">&#39;k&#39;</span><span class="p">:</span> <span class="s">&#39;5&#39;</span><span class="p">,</span> <span class="s">&#39;l&#39;</span><span class="p">:</span> <span class="s">&#39;5&#39;</span><span class="p">,</span> <span class="s">&#39;m&#39;</span><span class="p">:</span> <span class="s">&#39;6&#39;</span><span class="p">,</span>
         <span class="s">&#39;n&#39;</span><span class="p">:</span> <span class="s">&#39;6&#39;</span><span class="p">,</span> <span class="s">&#39;o&#39;</span><span class="p">:</span> <span class="s">&#39;6&#39;</span><span class="p">,</span> <span class="s">&#39;p&#39;</span><span class="p">:</span> <span class="s">&#39;7&#39;</span><span class="p">,</span> <span class="s">&#39;q&#39;</span><span class="p">:</span> <span class="s">&#39;7&#39;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">:</span> <span class="s">&#39;7&#39;</span><span class="p">,</span> <span class="s">&#39;s&#39;</span><span class="p">:</span> <span class="s">&#39;7&#39;</span><span class="p">,</span> <span class="s">&#39;t&#39;</span><span class="p">:</span> <span class="s">&#39;8&#39;</span><span class="p">,</span>
         <span class="s">&#39;u&#39;</span><span class="p">:</span> <span class="s">&#39;8&#39;</span><span class="p">,</span> <span class="s">&#39;v&#39;</span><span class="p">:</span> <span class="s">&#39;8&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">:</span> <span class="s">&#39;9&#39;</span><span class="p">,</span> <span class="s">&#39;x&#39;</span><span class="p">:</span> <span class="s">&#39;9&#39;</span><span class="p">,</span> <span class="s">&#39;y&#39;</span><span class="p">:</span> <span class="s">&#39;9&#39;</span><span class="p">,</span> <span class="s">&#39;z&#39;</span><span class="p">:</span> <span class="s">&#39;9&#39;</span><span class="p">}</span>
    <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">char2number</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">phone</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="n">phone2numeric</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">phone2numeric</span><span class="p">)</span>


<span class="c"># From http://www.xhaus.com/alan/python/httpcomp.html#gzip</span>
<span class="c"># Used with permission.</span>
<span class="k">def</span> <span class="nf">compress_string</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">zbuf</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
    <span class="n">zfile</span> <span class="o">=</span> <span class="n">GzipFile</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s">&#39;wb&#39;</span><span class="p">,</span> <span class="n">compresslevel</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">fileobj</span><span class="o">=</span><span class="n">zbuf</span><span class="p">)</span>
    <span class="n">zfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="n">zfile</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">zbuf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">StreamingBuffer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vals</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="n">b</span><span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vals</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vals</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="n">ret</span>

    <span class="k">def</span> <span class="nf">flush</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>


<span class="c"># Like compress_string, but for iterators of strings.</span>
<span class="k">def</span> <span class="nf">compress_sequence</span><span class="p">(</span><span class="n">sequence</span><span class="p">):</span>
    <span class="n">buf</span> <span class="o">=</span> <span class="n">StreamingBuffer</span><span class="p">()</span>
    <span class="n">zfile</span> <span class="o">=</span> <span class="n">GzipFile</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s">&#39;wb&#39;</span><span class="p">,</span> <span class="n">compresslevel</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">fileobj</span><span class="o">=</span><span class="n">buf</span><span class="p">)</span>
    <span class="c"># Output headers...</span>
    <span class="k">yield</span> <span class="n">buf</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">:</span>
        <span class="n">zfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="n">zfile</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
        <span class="k">yield</span> <span class="n">buf</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">zfile</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">yield</span> <span class="n">buf</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>

<span class="n">ustring_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&quot;([</span><span class="se">\u0080</span><span class="s">-</span><span class="se">\uffff</span><span class="s">])&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">javascript_quote</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">quote_double_quotes</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s">&quot;django.utils.text.javascript_quote() is deprecated. &quot;</span>
        <span class="s">&quot;Use django.utils.html.escapejs() instead.&quot;</span>
    <span class="p">)</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">RemovedInDjango19Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">fix</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="se">\\</span><span class="s">u</span><span class="si">%04x</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">ord</span><span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">==</span> <span class="nb">bytes</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">!=</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\\\</span><span class="s">&#39;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\r</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">r&#39;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">n&#39;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\t</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">t&#39;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot;&#39;&quot;</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\\</span><span class="s">&#39;&quot;</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&lt;/&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;</span><span class="se">\\</span><span class="s">/&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">quote_double_quotes</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;quot;&#39;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ustring_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">fix</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="n">javascript_quote</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">javascript_quote</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>

<span class="c"># Expression to match some_token and some_token=&quot;with spaces&quot; (and similarly</span>
<span class="c"># for single-quoted strings).</span>
<span class="n">smart_split_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&quot;&quot;&quot;</span>
<span class="s">    ((?:</span>
<span class="s">        [^\s&#39;&quot;]*</span>
<span class="s">        (?:</span>
<span class="s">            (?:&quot;(?:[^&quot;\\]|\\.)*&quot; | &#39;(?:[^&#39;\\]|\\.)*&#39;)</span>
<span class="s">            [^\s&#39;&quot;]*</span>
<span class="s">        )+</span>
<span class="s">    ) | \S+)</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">VERBOSE</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">smart_split</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">r&quot;&quot;&quot;</span>
<span class="sd">    Generator that splits a string by spaces, leaving quoted phrases together.</span>
<span class="sd">    Supports both single and double quotes, and supports escaping quotes with</span>
<span class="sd">    backslashes. In the output, strings will keep their initial and trailing</span>
<span class="sd">    quote marks and escaped quotes will remain escaped (the results can then</span>
<span class="sd">    be further processed with unescape_string_literal()).</span>

<span class="sd">    &gt;&gt;&gt; list(smart_split(r&#39;This is &quot;a person\&#39;s&quot; test.&#39;))</span>
<span class="sd">    [&#39;This&#39;, &#39;is&#39;, &#39;&quot;a person\\\&#39;s&quot;&#39;, &#39;test.&#39;]</span>
<span class="sd">    &gt;&gt;&gt; list(smart_split(r&quot;Another &#39;person\&#39;s&#39; test.&quot;))</span>
<span class="sd">    [&#39;Another&#39;, &quot;&#39;person\\&#39;s&#39;&quot;, &#39;test.&#39;]</span>
<span class="sd">    &gt;&gt;&gt; list(smart_split(r&#39;A &quot;\&quot;funky\&quot; style&quot; test.&#39;))</span>
<span class="sd">    [&#39;A&#39;, &#39;&quot;\\&quot;funky\\&quot; style&quot;&#39;, &#39;test.&#39;]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">bit</span> <span class="ow">in</span> <span class="n">smart_split_re</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">bit</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_replace_entity</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">text</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;#&#39;</span><span class="p">:</span>
        <span class="n">text</span> <span class="o">=</span> <span class="n">text</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">text</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="s">&#39;xX&#39;</span><span class="p">:</span>
                <span class="n">c</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">text</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="mi">16</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">c</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">six</span><span class="o">.</span><span class="n">unichr</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">six</span><span class="o">.</span><span class="n">unichr</span><span class="p">(</span><span class="n">html_entities</span><span class="o">.</span><span class="n">name2codepoint</span><span class="p">[</span><span class="n">text</span><span class="p">])</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">KeyError</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

<span class="n">_entity_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&quot;&amp;(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">unescape_entities</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">_entity_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">_replace_entity</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="n">unescape_entities</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">unescape_entities</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">unescape_string_literal</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">r&quot;&quot;&quot;</span>
<span class="sd">    Convert quoted string literals to unquoted strings with escaped quotes and</span>
<span class="sd">    backslashes unquoted::</span>

<span class="sd">        &gt;&gt;&gt; unescape_string_literal(&#39;&quot;abc&quot;&#39;)</span>
<span class="sd">        &#39;abc&#39;</span>
<span class="sd">        &gt;&gt;&gt; unescape_string_literal(&quot;&#39;abc&#39;&quot;)</span>
<span class="sd">        &#39;abc&#39;</span>
<span class="sd">        &gt;&gt;&gt; unescape_string_literal(&#39;&quot;a \&quot;bc\&quot;&quot;&#39;)</span>
<span class="sd">        &#39;a &quot;bc&quot;&#39;</span>
<span class="sd">        &gt;&gt;&gt; unescape_string_literal(&quot;&#39;\&#39;ab\&#39; c&#39;&quot;)</span>
<span class="sd">        &quot;&#39;ab&#39; c&quot;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">&#39;&quot;</span> <span class="ow">or</span> <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Not a string literal: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">s</span><span class="p">)</span>
    <span class="n">quote</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">r&#39;\</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">quote</span><span class="p">,</span> <span class="n">quote</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">r&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">)</span>
<span class="n">unescape_string_literal</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">unescape_string_literal</span><span class="p">)</span>


<div class="viewcode-block" id="slugify"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.text.slugify">[docs]</a><span class="k">def</span> <span class="nf">slugify</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts to ASCII. Converts spaces to hyphens. Removes characters that</span>
<span class="sd">    aren&#39;t alphanumerics, underscores, or hyphens. Converts to lowercase.</span>
<span class="sd">    Also strips leading and trailing whitespace.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">unicodedata</span><span class="o">.</span><span class="n">normalize</span><span class="p">(</span><span class="s">&#39;NFKD&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">,</span> <span class="s">&#39;ignore&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;[^\w\s-]&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;[-\s]+&#39;</span><span class="p">,</span> <span class="s">&#39;-&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span></div>
<span class="n">slugify</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">slugify</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">camel_case_to_spaces</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Splits CamelCase and converts to lower case. Also strips leading and</span>
<span class="sd">    trailing whitespace.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">re_camel_case</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39; \1&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.text</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>